// pages/me/offerPrice/offerPrice.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        searchParams:{
            pageNum: 1,
            pageSize: 6,
            queryType:'3',
            carId:'',
            pages:'',
            total:'',
        },
        noData: true,
        InitData:false,
        offerPriceList:{},

    },

    doNothing(){},
    onLoad: function (options) {
        // 必须一个车辆的ID
        if(undefined != options.cid){
            let self = this;
            this.setData({
                'searchParams.carId': options.cid
            },()=>{
                self.InitUserStatus();
                self.setData({
                  'InitData': true
                })
            })
        }else{
            app.showNoAuthOrNoGrant('未找到您要查看的出价信息~');
        }
    },
    // 检查一下登录状态。
    async InitUserStatus(){
        app.showLoading("获取用户信息");
        await app.checkToken();
        this.InitUserInfo();
        wx.hideLoading();
    },
    InitUserInfo(){
        let userInfo = app.globalData.userInfo;
        // 这里再验证一遍权限。
        if (userInfo.roleList.indexOf('viewCar') == -1) {
            app.showNoAuthOrNoGrant('您无权操作查看此页面');
            return;
        }else{
            this.goQuery();
        }
    },
    goQuery(){
      this.setData({
        'searchParams.pageNum':1,
        'searchParams.total':'',
        'searchParams.pages':'',
        'offerPriceList': {}
      });
      this.carManageOfferPriceList(this.data.searchParams);
    },
    carManageOfferPriceList(searchParams){
      // 整理完成进行搜索操作。
      app.sHttp.carManageOfferPriceList(searchParams).then(res=>{
          let offerPriceList = this.data.offerPriceList;
          offerPriceList[searchParams.pageNum] = res.result.offerPriceList;
          if (0 == res.result.total) {
            this.setData({
              'noData': true
            })
          }else{
              this.setData({
                  'noData': false,
                  'offerPriceList': offerPriceList,
                  'searchParams.total':res.result.total,
                  'searchParams.pages': res.result.pages
                });
          }
        },err=>{
          app.error(err.msg);
        })
    },
    onReachBottom(){
        if (this.data.searchParams.pageNum == this.data.searchParams.pages) {
            //app.alert('没有更多了哦~');
        }else{
            let self = this;
            this.setData({
                'searchParams.pageNum': self.data.searchParams.pageNum+1
            });
            this.carManageOfferPriceList(this.data.searchParams);
        }
    },
    makePhoneCall(e){
        let page = e.currentTarget.dataset.page;
        let index = e.currentTarget.dataset.idx;
        let item = this.data.offerPriceList[page][index];
        if(0 == item.status){
            wx.showModal({
                content: '您确定要处理该出价嘛？',
                showCancel: true,
                title: '处理提醒',
                success: (result) => {
                    if (result.confirm) {
                        // 处理确认。
                        app.sHttp.carManageOfferPriceHandle({carId: item.carId,detailId:item.detailId}).then(res=>{
                            // 重新加载当前页面
                            this.carManageOfferPriceList({
                                pageNum: page,
                                pageSize: 6,
                                queryType:this.data.searchParams.queryType,carId:this.data.searchParams.carId});
                            wx.makePhoneCall({
                                phoneNumber: item.userMobile,
                                success:function(res){
                                    console.log(res);
                                },
                                fail:function(){}
                            });
                        },err=>{
                            app.error(err.msg);
                        })
                    }
                },
                fail: (res) => {}
              })
        }else{
            wx.makePhoneCall({
                phoneNumber: item.userMobile,
                success:function(res){
                    console.log(res);
                },
                fail:function(){}
            })
        }
    },
    seeCarMsg(e){
        let cid = e.currentTarget.dataset.cid;
        wx.navigateTo({
          url: '/pages/card/card?cid=' + cid,
        })
    },
})